<?php
namespace Badmins\Controller;
class pluginController extends CkadminController {
    public function pay(){
    	//支付宝配置
    	$zhifubao = F('set_zhifubao');
    	//微信支付配置
    	$weixin = F('set_weixin');
    	//银联支付
    	$yinlian = F('set_yinlian');
    	//财付通支付
    	$caifutong = F('set_caifutong');
    	$this->assign('z',$zhifubao);
    	$this->assign('w',$weixin);
    	$this->assign('y',$yinlian);
    	$this->assign('c',$caifutong);
    	$this->display();
    }
    /**
     * 保存各种支付
     */
    public function save_pay(){
    	$type = I('type');
    	$post = I('post.');
    	switch ($type){
    		case '1':
    			F('set_zhifubao',$post);
    		break;
    		case '2':
    			F('set_zhifubao',$post);
    		break;
    		case '3':
    			F('set_weixin',$post);
    		break;
    		case '4':
    			F('set_yinlian',$post);
    		break;
    		case '5':
    			F('set_caifutong',$post);
    		break;
    	}
    	showmsg(200,'保存成功');
    }
    /**
     * 改变支付的状态
     */
    public function update_pay(){
    	$id = I('id/d');
    	$db = M('ad');
    	$type = I('type');
    	switch ($type){
    		case '1':
    			$list = F('set_zhifubao');
    			$name = "支付宝支付";
    			$fname = "set_zhifubao";
    			break;
    		case '2':
    			$list = F('set_zhifubao');
    			$name = "支付宝支付";
    			$fname = "set_zhifubao";
    			break;
    		case '3':
    			$list = F('set_weixin');
    			$name = "微信支付";
    			$fname = "set_weixin";
    			break;
    		case '4':
    			$list = F('set_yinlian');
    			$name = "银联支付";
    			$fname = "set_yinlian";
    			break;
    		case '5':
    			$list = F('set_caifutong');
    			$name = "财付通支付";
    			$fname = "set_caifutong";
    			break;
    	}
    	if($list['status'] == 1){
    		$list['status'] = 0;
    		F($fname,$list);
    		//$db->where("id='{$id}'")->setField('status',0);
    		$log = "将名为".$name."的状态有关闭变成开启";
    	}else{
    		$list['status'] = 1;
    		F($fname,$list);
    		$log = "将名为".$name."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 测试
     */
    public function ceshi(){
    	$urll = "http://".$_SERVER['HTTP_HOST']."/Wxpay/notify";
    	//import("Org.Wx.wxpayapi");
    	//import("Org.Wx.jsapipay");
    	if(strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')){
    		vendor("Wxpay.wxpayapi");
    		vendor('Wxpay.jsapipay');
    		//import("Org.Wx.log",'','.php');
    		$tools = new \JsApiPay();
	    	$openId = $tools->GetOpenid($urll);
	    	//②、统一下单
	    	$input = new \WxPayUnifiedOrder();
	    	$input->SetBody("订单支付");
	    	$input->SetAttach("test");
	    	$input->SetOut_trade_no("1231231");
	    	$input->SetTotal_fee(1*100);
	    	$input->SetTime_start(date("YmdHis"));
	    	$input->SetTime_expire(date("YmdHis", time() + 600));
	    	$input->SetGoods_tag("test");
	    	$input->SetNotify_url($urll);
	    	$input->SetTrade_type("JSAPI");
	    	$input->SetOpenid($openId);
	    	//print_r($input);exit();
	    	$order = \WxPayApi::unifiedOrder($input);
	    	$this->jsApiParameters = $tools->GetJsApiParameters($order);
    	}else{
    		//扫码支付
    		vendor("Wxpay.nativepay",'','.class.php');
    		//import("Org.Wx.nativepay");
    		//import("Org.Wx.log",'','.php');
    		$notify = new \NativePay();
    		$input = new \WxPayUnifiedOrder();
    		$input->SetBody("订单支付");
    		$input->SetAttach(date("YmdHis").rand(100,999));
    		$input->SetOut_trade_no(date("YmdHis").rand(100,999));
    		$input->SetTotal_fee(100);
    		$input->SetTime_start(date("YmdHis"));
    		$input->SetTime_expire(date("YmdHis", time() + 600));
    		$input->SetGoods_tag("test");
    		$input->SetNotify_url($urll);
    		$input->SetTrade_type("NATIVE");
    		$input->SetProduct_id(date("YmdHis").rand(100,999));
    		$result = $notify->GetPayUrl($input);
    		$url2 = $result["code_url"];
    		//print_r($result);exit();
    		//import("Org.phpqrcode.phpqrcode",'','.php');
    		vendor("phpqrcode.phpqrcode");
    		$path="./Public/Uploads/code/";
    		$name=date("YmdHis").rand(100,999).".png";
    		$level = 'H';
    		$size = 4;
    		$filename = $path.$name;
    		\QRcode::png($url2,$filename,$level,$size);
    		$this->photo= "http://".$_SERVER['HTTP_HOST']."/Public/Uploads/code/".$name;
    	}
    }
    public function notify()
    {
    	//使用通用通知接口
    	$notify = new \Notify_pub();
    	//存储微信的回调
    	$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
    	$notify->saveData($xml);
    	//          var_dump($xml);
    	//验证签名，并回应微信。
    	//对后台通知交互时，如果微信收到商户的应答不是成功或超时，微信认为通知失败，
    	//微信会通过一定的策略（如30分钟共8次）定期重新发起通知，
    	//尽可能提高通知的成功率，但微信不保证通知最终能成功。
    	if($notify->checkSign() == FALSE){
    		$notify->setReturnParameter("return_code","FAIL");//返回状态码
    		$notify->setReturnParameter("return_msg","签名失败");//返回信息
    	}else{
    		$notify->setReturnParameter("return_code","SUCCESS");//设置返回码
    	}
    	$returnXml = $notify->returnXml();
    	echo $returnXml;
    	 
    	//==商户根据实际情况设置相应的处理流程，此处仅作举例=======
    	 
    	//以log文件形式记录回调信息
    	//         $log_ = new Log_();
    	$log_name= __ROOT__."/Public/notify_url.log";//log文件路径
    	 
    	$this->log_result($log_name,"【接收到的notify通知】:\n".$xml."\n");
    	 
    	if($notify->checkSign() == TRUE)
    	{
    		if ($notify->data["return_code"] == "FAIL") {
    			//此处应该更新一下订单状态，商户自行增删操作
    			log_result($log_name,"【通信出错】:\n".$xml."\n");
    			$this->error("1");
    		}
    		elseif($notify->data["result_code"] == "FAIL"){
    			//此处应该更新一下订单状态，商户自行增删操作
    			log_result($log_name,"【业务出错】:\n".$xml."\n");
    			$this->error("失败2");
    		}
    		else{
    			//此处应该更新一下订单状态，商户自行增删操作
    			log_result($log_name,"【支付成功】:\n".$xml."\n");
    			$this->success("支付成功！");
    		}
    		 
    		//商户自行增加处理流程,
    		//例如：更新订单状态
    		//例如：数据库操作
    		//例如：推送支付完成信息
    	}
    }
}